package com.gxa.insurance.controller;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.mapper.PolicyMapper;
import com.gxa.insurance.pojo.Customer;
import com.gxa.insurance.pojo.Information;
import com.gxa.insurance.pojo.Policy;
import com.gxa.insurance.pojo.Variety;
import com.gxa.insurance.service.InformationSerivce;
import com.gxa.insurance.service.PolicyService;
import com.gxa.insurance.service.VarietyService;
import com.gxa.insurance.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @date 2022/6/6
 */
@Api(value = "保险模块policy相关的api接口")
@RestController
@RequestMapping("/policy")
@CrossOrigin
public class PolicyController {

    @Autowired
    PolicyService policyService;

    @ApiOperation(value = "添加保单",produces = "query")
    @PostMapping("/add")
   public Map add(@RequestBody Policy policy,
                  @ApiIgnore HttpSession session) throws BusinessException {

        Integer add = policyService.add(policy);

        JsonResult jsonResult =new JsonResult();
       jsonResult.setCode("200");
       jsonResult.setMsg("添加成功");
       jsonResult.setData(add);

       return jsonResult.getMap();
   }

   @Autowired
    InformationSerivce informationSerivce;

    @ApiOperation(value = "添加保单信息",produces = "query")
    @PostMapping("/insert")
    public Map insert(@RequestBody Information information){

        Integer add = informationSerivce.add(information);

        JsonResult jsonResult =new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        jsonResult.setData(add);

        return jsonResult.getMap();
    }

   @Autowired
    VarietyService varietyService;

    @ApiOperation(value = "保险种类搜索(用于添加使用)",produces = "query")
    @ApiImplicitParam(name = "name",value = "保险种类名字",paramType = "query")
    @PostMapping("/selectByVarietyName")
   public Map selectByVarietyName(String name
//            ,@RequestHeader(value = "token",required = false) String token
    ) throws BusinessException {

       List<Variety> varieties = varietyService.selectByName(name);

       JsonResult jsonResult=new JsonResult();
       jsonResult.setCode("200");
       jsonResult.setMsg("查询保险种类成功");
       jsonResult.setData(varieties);

       return jsonResult.getMap();
   }


    @ApiOperation(value = "修改保单信息表",produces = "query")
    @PostMapping("/updateInformation")
    public Map updateInformation(@RequestBody Information information){

        informationSerivce.update(information);

        JsonResult jsonResult =new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");

        return jsonResult.getMap();
    }


   @ApiOperation(value = "删除保单",produces = "query")
   @ApiImplicitParam(name ="id",value = "用户编号",paramType = "query")
   @PostMapping("/delete")
   public Map delete(Integer id,
                     @ApiIgnore HttpSession session) throws BusinessException {

        policyService.delete(id);

       JsonResult jsonResult =new JsonResult();
       jsonResult.setCode("200");
       jsonResult.setMsg("删除成功");


        return jsonResult.getMap();
   }

    @ApiOperation(value = "删除保单信息表",produces = "query")
    @ApiImplicitParam(name ="name",value = "用户名字",paramType = "query")
    @PostMapping("/deleteInformation")
    public Map deleteInformation(String name,
                      @ApiIgnore HttpSession session) throws BusinessException {

       informationSerivce.delete(name);

        JsonResult jsonResult =new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "用户保单列表",produces = "query")
    @PostMapping("/selectAll")
    public Map selectAll(
//            @RequestHeader(value = "token",required = false) String token,
                         @ApiIgnore HttpSession session) throws BusinessException {

        Map<String, Object> map = policyService.selectAll();
        Object count = map.get("count");
        Object policies = map.get("policies");

        JsonResult jsonResult =new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.put("count",count);
        jsonResult.setData(policies);

        return jsonResult.getMap();
    }

    @ApiOperation(value = "订单搜索",produces = "query")
    @ApiImplicitParam(name = "name",value = "手机号或姓名",paramType = "query")
    @PostMapping("/selectByName")
    public Map selectByName(String name) throws BusinessException {

        List<Policy> policies = policyService.selectLikeName(name);

        JsonResult jsonResult =new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.put("count",policies.size());
        jsonResult.setData(policies);

        return jsonResult.getMap();
    }

    @ApiOperation(value = "根据id进行查询(配合修改使用)")
    @ApiImplicitParam(name = "id",value = "编号",dataType = "int",paramType = "query")
    @PostMapping("/selectById")
    public Map selectById(@RequestParam("id") Integer id) throws Exception {

        Policy policy = policyService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(policy);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "修改保单",produces = "query")
    @PostMapping("/update")
    public Map update(@RequestBody Policy policy,
                      @ApiIgnore HttpSession session) throws BusinessException {



        policyService.update(policy);

        JsonResult jsonResult =new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");

        return jsonResult.getMap();
    }

}
